package com.ossean.match.matchprocess;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MatchIncrement extends Thread implements Runnable {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private int memoHistory; // the record of memo after matching last time
	private static File record; // record file "record.txt"
	private boolean newPrjComing;  //新项目进入标识
	private boolean newMemoComing;  //新帖子进入标识
	private boolean currentMatchDone = true;  //当前批量匹配是否结束
	private int sleepTime = 0;
	
	public boolean isNewPrjComing() {
		return newPrjComing;
	}

	public void setNewPrjComing(boolean newPrjComing) {
		this.newPrjComing = newPrjComing;
	}

	public boolean isNewMemoComing() {
		return newMemoComing;
	}

	public void setNewMemoComing(boolean newMemoComing) {
		this.newMemoComing = newMemoComing;
	}

	public int getSleepTime() {
		return sleepTime;
	}

	public void setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
	}

	public MatchIncrement() {
		record = new File("record.txt");
		getMatchRecord();
	}

	public int getMemoHistory() {
		return memoHistory;
	}

	public synchronized void setMemoHistory(int memoHistory) {
		this.memoHistory = memoHistory;
	}

	public boolean isCurrentMatchDone() {
		return currentMatchDone;
	}

	public synchronized void setCurrentMatchDone(boolean currentMatchDone) {
		this.currentMatchDone = currentMatchDone;
	}

	/**
	 * read the record after matching last time from record file "record.txt",
	 * the format of the record in the file: "project's record" "memo's record"
	 */
	public void getMatchRecord() {
		try {
			Scanner in = new Scanner(record);
			this.setMemoHistory(in.nextInt());
			in.close();
		} catch (FileNotFoundException e) {
			logger.error("getMatchRecord FileNotFoundException： " + e);
		}
	}

	/**
	 * write the result to the record file
	 */
	public void writeIntoRecord() {
		try {
			FileOutputStream fout = new FileOutputStream(record);
			fout.write((memoHistory + "").getBytes());
			fout.close();
		} catch (FileNotFoundException e) {
			logger.error("writeIntoRecord FileNotFoundException： " + e);
		} catch (IOException e) {
			logger.error("writeIntoRecord FileNotFoundException： " + e);
		}
	}
}